package cn.amarone.common.exception;

import cn.amarone.common.core.domain.R;
import cn.amarone.common.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Amarone
 * @version 1.0
 * @description: 全局异常处理
 * @date 2022/9/14 15:30
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request) {
        log.error("请求地址'{}',发生系统异常.", request.getRequestURI(), e);
        String activeProfile = SpringUtils.getActiveProfile();
        if ("dev".equals(activeProfile)) {
            return R.fail(e.getMessage());
        } else {
            return R.fail("请求异常，请稍后重试!");
        }
    }
}
